;function Signature(param){
	this.param = param;
	this.init();
	this.paintInit({});
	this.addListener();
	
};
/**
 * 对象初始化
 */
Signature.prototype.init = function(){
	var clientWidth = document.body.clientWidth || document.documentElement.clientWidth;
	var clientHeight= document.body.clientHeight || document.documentElement.clientHeight;
	var con = document.createElement('div');
	con.id = 'sign-con';
	con.className = 'sign-con';
	con.style.height = clientHeight + 'px';
	document.body.appendChild(con);
	var arcHtml = 
			'<div class="sign-footer"><div class="cell-top" id="rewrite"><span>重写</span></div><div class="cell-bottom" id="signcommit"><span>提交</span></div></div>'+
			'<div class="sign-context"><canvas id="sign-canvas" width="320" height="200"></canvas></div>'+
			'<div class="sign-header"><span class="title" id="sign-title">电子签名</span><div class="close-con" id="sign-close"><i class="addiconfont icon-123"></i></div></div>';
	con.innerHTML = arcHtml;
	this.con = con;
	this.canvas = document.getElementById('sign-canvas');
	this.canvas.width = clientWidth - 35 - 60;
	this.canvas.height = clientHeight;
	this.elemPosition(clientWidth,clientHeight);
	this.ctx = this.canvas.getContext('2d');
};
//设置监听事件
Signature.prototype.addListener = function(){
	var _this = this;
	var point ={
			boxTop:0,
			boxLeft:0,
			startX:0,
			startY:0,
	};
	var eventObj = {
		touchStart:function(ev){
					ev.preventDefault();
					var box = _this.canvas.getBoundingClientRect();
					var targetTouch = ev.targetTouches[0];
					point.boxTop = box.top;
					point.boxLeft = box.left;
					point.startX = targetTouch.clientX;
					point.startY = targetTouch.clientY;
					_this.ctx.beginPath();
					_this.ctx.moveTo(point.startX-point.boxLeft,point.startY-point.boxTop);
					_this.ctx.lineTo(targetTouch.clientX-point.boxLeft,targetTouch.clientY - point.boxTop);
					_this.ctx.stroke();			
		},
		touchMove:function(ev){
					ev.preventDefault();
					var targetTouch = ev.targetTouches[0];
					_this.ctx.beginPath();
					_this.ctx.moveTo(point.startX-point.boxLeft,point.startY-point.boxTop);
					_this.ctx.lineTo(targetTouch.clientX-point.boxLeft,targetTouch.clientY - point.boxTop);
					_this.ctx.stroke();
					point.startX = targetTouch.clientX;
					point.startY = targetTouch.clientY;			
		},
		touchEnd:function(ev){
			
		},
		controlEvent:function(ev){
			var targetId = ev.target.id;
			if(targetId == 'sign-close'){
				_this.close();
			}else if(targetId == 'rewrite'){
				_this.ctx.clearRect(0,0,_this.canvas.width,_this.canvas.height);
			}else if(targetId == 'signcommit'){
				var signData = _this.canvas.toDataURL();
				_this.param.cb.call(null,signData);
			};
		}
	};
	this.eventObj = eventObj;
	this.canvas.addEventListener('touchstart',eventObj.touchStart,false);
	this.canvas.addEventListener('touchmove',eventObj.touchMove,false);
	this.con.addEventListener('click',eventObj.controlEvent,false);
};
/**
 * 画笔初始化
 * @param {Object} opt
 */
Signature.prototype.paintInit = function(opt){
	var paintOpt = opt || {};
	this.ctx.lineJoin = 'round';
	this.ctx.lineCap = 'round';
	this.ctx.strokeStyle = opt.strokeStyle || '#000';
	this.ctx.lineWidth = opt.lineWidth || 3;
	
};
//移除监听
Signature.prototype.removeListener = function(){
	this.canvas.removeEventListener('touchstart',this.eventObj.touchStart);
	this.canvas.removeEventListener('touchmove',this.eventObj.touchMove);
	this.con.removeEventListener('click',this.eventObj.controlEvent);
};
//关闭签名器
Signature.prototype.close = function(){
	this.removeListener();
	document.body.removeChild(this.con);
};
/**
 * 根据屏幕的width和height定位
 * @param {Object} cw
 * @param {Object} ch
 */
Signature.prototype.elemPosition = function(cw,ch){
	var title = document.getElementById('sign-title');
	title.style.width = cw + 'px';
	title.style.left = (-(cw-35)/2)+'px';
	var rewrite = document.getElementById('rewrite');
	var signcommit = document.getElementById('signcommit');
	rewrite.style.lineHeight = Math.round(ch/2) + 'px';
	signcommit.style.lineHeight = Math.round(ch/2) + 'px';
};
